package cn.sdormitory.controller;

import cn.sdormitory.basedata.entity.BClass;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.basedata.vo.BClassVo;
import cn.sdormitory.basedata.vo.BDormitoryVO;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.school.entity.SchoolDuty;
import cn.sdormitory.school.entity.SchoolPatrol;
import cn.sdormitory.school.service.SchoolDutyService;
import cn.sdormitory.school.service.SchoolPatrolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 值班
 */
@RestController
@RequestMapping("/app/duty")
@Api("公共服务")
public class AppDutyController {

    @Autowired
    private SchoolPatrolService schoolPatrolService;


    @ApiOperation("getSchoolPatrolPageList => 分页查询巡查列表")
    @PostMapping(value = "/getSchoolPatrolPageList")
    public CommonResult<CommonPage<SchoolPatrol>> getSchoolPatrolPageList(@RequestBody Map<String, Object> params){
        return CommonResult.success(schoolPatrolService.getPage(params));
    }

    @ApiOperation("createOrUpdatePatrolInfo => 新增or修改巡查信息")
    @PostMapping(value = "/createOrUpdatePatrolInfo")
    public CommonResult<CommonPage<SchoolPatrol>> createOrUpdatePatrolInfo(@RequestBody SchoolPatrol schoolPatrol){
        CommonResult<CommonPage<SchoolPatrol>> failed = checkPatrolParam(schoolPatrol);
        if (failed != null) return failed;
        int num = schoolPatrolService.createOrUpdatePatrolInfo(schoolPatrol);
        if(num > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("deletePatrolInfo => 删除巡查信息")
    @GetMapping(value = "/deletePatrolInfo")
    public CommonResult<CommonPage<SchoolPatrol>> deletePatrolInfo(@RequestParam("id") Long id){

        int num = schoolPatrolService.deletePatrolInfo(id);
        if(num > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("getClassListByDept => 查询值班部门对应的班级")
    @GetMapping(value = "/getClassListByDept")
    public CommonResult<List<BClassVo>> getClassListByDept(@RequestParam("dutyDept") String dutyDept){
        List<BClassVo> classList =  schoolPatrolService.getClassListByDept(dutyDept);

        return CommonResult.success(classList);
    }

    private CommonResult<CommonPage<SchoolPatrol>> checkPatrolParam(SchoolPatrol schoolPatrol) {
        if("3".equals(schoolPatrol.getPatrolType()) && StringUtils.isEmpty(schoolPatrol.getClassPatrolJson())){
           return CommonResult.failed("课堂巡查请上传违纪json数据，没有违纪请传0");
        }
        if("2".equals(schoolPatrol.getPatrolType())){
            if(StringUtils.isEmpty(schoolPatrol.getClassId()) || StringUtils.isEmpty(schoolPatrol.getStudentNo())
                    || StringUtils.isEmpty(schoolPatrol.getDvType())){
                return CommonResult.failed("缺少必填字段");
            }
        }
        if("1".equals(schoolPatrol.getPatrolType())){
            if(StringUtils.isEmpty(schoolPatrol.getPatrolStatus())){
                return CommonResult.failed("缺少必填字段");
            }
        }
        if("4".equals(schoolPatrol.getPatrolType())){
            if(StringUtils.isEmpty(schoolPatrol.getPatrolStatus())){
                return CommonResult.failed("缺少必填字段");
            }
        }
        return null;
    }

    @ApiOperation("GetDormitoryNameByName => 查询宿舍信息")
    @GetMapping(value = "/getDormitoryNameByName")
    public CommonResult<List<BDormitoryVO>> getDormitoryNameByName(@RequestParam("name") String name){
        List<BDormitoryVO> classList =  schoolPatrolService.getDormitoryNameByName(name);

        return CommonResult.success(classList);
    }

    @ApiOperation("getSchoolPatrolDetails => 查询巡查详情")
    @GetMapping(value = "/getSchoolPatrolDetails")
    public CommonResult<SchoolPatrol> getSchoolPatrolDetails(@RequestParam Long id){
        SchoolPatrol schoolPatrol = schoolPatrolService.getSchoolPatrolDetails(id);
        return CommonResult.success(schoolPatrol);
    }

}
